using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Compression;

namespace MyWords
{
    public partial class WordEditor : UserControl
    {
        DateTime theDay = DateTime.Today;
        Word theWord = new Word();
        bool isLoading = true;

        public WordEditor()
        {
            InitializeComponent();
        }

        private void WordEditor_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                this.Start();
            }
        }

        private void Start()
        {
            bool status = this.Active;
            this.Active = false;

            DateTime dt = this.dateTimePicker1.Value.Date;
            this.LoadWordsOfTheDay(dt);
            this.headTextBox.Focus();

            this.Active = status;
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            this.Start();

            //DateTime dt = this.dateTimePicker1.Value.Date;
            //if (dt != this.theDay)
            //{
            //    this.LoadWordsOfTheDay(dt);
            //}
        }

        private void LoadWordsOfTheDay(DateTime dateTime)
        {
            this.isLoading = true;

            this.theDay = dateTime.Date;

            WordList wordList1 = DB.LoadWords(this.theDay, this.theDay.AddDays(1)); //DB.LoadScheduledWords(this.theDay);
            this.listBox1.Items.Clear();
            foreach (Word aWord in wordList1)
            {
                this.listBox1.Items.Add(aWord);
            }

            if (this.listBox1.Items.Count > 0)
                this.listBox1.SelectedIndex = 0;
            else
                this.listBox1.SelectedIndex = -1;

            this.isLoading = false;

            this.listBox1_SelectedIndexChanged(null, null);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.isLoading) return;

            this.timer1.Stop();
            if (this.checkBoxAutoPlay.Checked) this.timer1.Start();
            if (this.listBox1.SelectedIndex >= 0)
            {
                this.theWord = (Word)this.listBox1.SelectedItem;
                Utility.PlayAudio(this.theWord);
            }
            else
            {
                this.theWord = new Word();
            }
            this.BindWord(this.theWord);
        }

        private void BindWord(Word aWord)
        {
            this.headTextBox.Text = aWord.Head;
            this.meaningTextBox.Text = aWord.Meaning;

            if (aWord.Head == "")
                this.buttonSave.Enabled = false;
            else
                this.buttonSave.Enabled = true;

            this.labelSchedule.Text = "";
            this.labelStudy.Text = "";

            foreach (Schedule aSchedule in aWord.Schedules)
            {
                if (aSchedule.StudyDate.IsNull)
                {
                    this.labelSchedule.Text += ((TimeSpan)(aSchedule.ScheduleDate.Value.Date - DateTime.Today)).TotalDays.ToString() + ",  ";
                }
                else
                {
                    this.labelStudy.Text += ((TimeSpan)(DateTime.Today - aSchedule.StudyDate.Value.Date)).TotalDays.ToString() + ",  ";
                }
            }
        }

        private void headTextBox_Leave(object sender, EventArgs e)
        {
            this.headTextBox.Text = this.headTextBox.Text.Trim();
            if (this.headTextBox.Text != this.theWord.Head)
            {
                this.theWord = DB.LoadWord(this.headTextBox.Text);
                if (this.theWord == null)
                {
                    this.theWord = new Word(this.headTextBox.Text, "");
                }
                else
                {
                    Utility.PlayAudio(this.theWord);
                }
                this.BindWord(this.theWord);
            }
        }

        private void meaningTextBox_Leave(object sender, EventArgs e)
        {
            this.meaningTextBox.Text = this.meaningTextBox.Text.Trim();
            this.theWord.Meaning = this.meaningTextBox.Text;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (this.theWord.Head.Length == 0) return;
            if (this.theWord.Schedules.Count == 0)
            {
                GenerateSchedule(this.theWord);
            }

            try
            {
                DB.SaveWord(this.theWord);
                this.BindWord(this.theWord);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            if (this.theDay == DateTime.Today && this.theWord.CreatedDate.Date == DateTime.Today)
            {
                this.isLoading = true;

                if (!this.listBox1.Items.Contains(this.theWord))
                {
                    this.listBox1.Items.Add(this.theWord);
                }
                this.listBox1.SelectedItem = this.theWord;

                this.isLoading = false;
            }
        }

        private void GenerateSchedule(Word aWord)
        {
            DateTime dt = DateTime.Now;
            if (this.radioButtonNormal.Checked)
            {
                aWord.Schedules.Add(new Schedule(dt.Date.AddDays(1)));
                aWord.Schedules.Add(new Schedule(dt.Date.AddDays(3)));
                aWord.Schedules.Add(new Schedule(dt.Date.AddDays(7)));
                aWord.Schedules.Add(new Schedule(dt.Date.AddDays(12)));
                aWord.Schedules.Add(new Schedule(dt.Date.AddDays(22)));
                aWord.Schedules.Add(new Schedule(dt.Date.AddDays(43)));
            }
            else
            {
                aWord.Schedules.Add(new Schedule(dt.Date.AddDays(1)));
                aWord.Schedules.Add(new Schedule(dt.Date.AddDays(3)));
                aWord.Schedules.Add(new Schedule(dt.Date.AddDays(12)));
                aWord.Schedules.Add(new Schedule(dt.Date.AddDays(43)));
            }
        }

        private void buttonPrev_Click(object sender, EventArgs e)
        {
            this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddDays(-1);
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddDays(1);
        }

        private void buttonRec_Click(object sender, EventArgs e)
        {
            if (this.theWord.Head.Length == 0) return;
            if (this.buttonRec.Text != "Stop")
            {
                this.buttonRec.BackColor = Color.Red;
                this.buttonRec.Text = "Stop";
                MyAudio.AudioRecorder.Rewind();
                MyAudio.AudioRecorder.StartRecord();
            }
            else
            {
                this.buttonRec.BackColor = Color.Gold;
                this.buttonRec.Text = "Rec Audio";
                MyAudio.AudioRecorder.StopRecord();
                byte[] audioBytes = MyAudio.AudioRecorder.SaveToBytes();

                this.theWord.Audio = AudioFormat.Original ;
                this.theWord.AudioBytes = audioBytes;
            }
        }



        private void buttonStopPlay_Click(object sender, EventArgs e)
        {
            Utility.StopAudio();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.listBox1.Items.Count > 1)
            {
                int i = this.listBox1.SelectedIndex + 1;
                i = i % this.listBox1.Items.Count;
                this.listBox1.SelectedIndex = i;
            }
        }

        public void Play()
        {
            this.checkBoxAutoPlay.Checked = true;
            Utility.PlayAudio(this.theWord);
        }
        public void Pause()
        {
            this.checkBoxAutoPlay.Checked = !this.checkBoxAutoPlay.Checked;
            if (this.checkBoxAutoPlay.Checked)
            {
                this.checkBoxAutoPlay.Checked = false;
                Utility.StopAudio();
            }
            else
            {
                this.Play();
            }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            this.timer1.Stop();
            this.timer1.Interval = Convert.ToInt32(this.numericUpDown1.Value) * 1000;
            if (this.checkBoxAutoPlay.Checked) this.timer1.Start();
        }

        private bool m_Active = false;
        public bool Active
        {
            get
            {
                return m_Active;
            }
            set
            {
                m_Active = value;
                if (!m_Active)
                {
                    this.timer1.Stop();
                    Utility.StopAudio();
                }
                else
                {
                    Utility.PlayAudio(theWord);
                    if (this.checkBoxAutoPlay.Checked) this.timer1.Start();
                }
            }
        }

        private void checkBoxAutoPlay_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBoxAutoPlay.Checked)
            {
                this.timer1.Start();
            }
            else
            {
                this.timer1.Stop();
            }
        }

        private void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (this.listBox1.SelectedIndex >= 0)
                {
                    bool status = this.Active;
                    this.Active = false;

                    Word aWord = (Word)this.listBox1.SelectedItem;
                    DB.DeleteWord(aWord.Head);

                    int cur = this.listBox1.SelectedIndex;
                    this.listBox1.Items.RemoveAt(cur);
                    if (this.listBox1.Items.Count > 0)
                    {
                        this.listBox1.SelectedIndex = cur % this.listBox1.Items.Count;
                    }

                    this.Active = status;
                }
            }
        }

    }
}
